﻿using System;
using System.Windows;
using System.Windows.Controls;
using HashCalculator.ViewModels;
using Microsoft.Win32;

namespace HashCalculator
{
    public partial class MainWindow : Window
    {
        HashCalculatorViewModel vm;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            vm = new HashCalculatorViewModel();
            vm.BrowseExecuted += (s, ev) =>
            {
                OpenFileDialog ofd = new OpenFileDialog { Filter = "All files (*.*)|*.*" };

                bool? res = ofd.ShowDialog();

                if (res.HasValue && res.Value && ofd.FileName.Length != 0)
                {
                    vm.FilePath = ofd.FileName;
                }
            };
            DataContext = vm;

            if (Environment.GetCommandLineArgs().Length > 1)
            {
                vm.FilePath = Environment.GetCommandLineArgs()[1];
                vm.ComputeCommand.Execute();
            }
        }

        private void Window_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effects = DragDropEffects.Copy;
                if (sender is TextBox)
                {
                    textBoxFilePath.IsHitTestVisible = false;
                }
            }
            else
            {
                e.Effects = DragDropEffects.None;
            }
        }

        private void Window_Drop(object sender, DragEventArgs e)
        {
            textBoxFilePath.IsHitTestVisible = true;
            var files = e.Data.GetData(DataFormats.FileDrop) as string[];
            if (files != null && files.Length > 0)
            {
                vm.FilePath = files[0];
            }
        }
    }
}
